package com.huarui.payInvoice.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.initial.util.InitialUtil;
import com.huarui.payInvoice.model.PayInvoice;
import com.huarui.payInvoice.service.PayInvoiceService;
import com.huarui.payInvoiceItem.model.PayInvoiceItem;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.opensymphony.xwork2.Action;

public class PayInvoiceAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer start;
	private Integer limit;
	private Integer id;
	private String sort;
	private String dir;
	private Integer customerId;
	@SuppressWarnings("unused")
	private String searchConditions;
	@SuppressWarnings("unchecked")
	private ListModel listModel = new ListModel();

	private PayInvoiceService payInvoiceService;
	private String payableIds;
	private String payInvoiceJson;
	private String payInvoiceItemJson;
	private Map<String,Object> resultMap=new HashMap<String, Object>();
	private SessionMessage sm;
	
	public String extList() {
		sm = UserHolder.getCurrentSessionMessage();
		listModel.setPageSize(limit == null ? 10 : limit);
		listModel.setPage(start == null ? 0 : start / limit + 1);
		listModel.setOrderBy(sort==null?"id":sort);
		if (dir != null){
			listModel.setDesc(dir.equals("DESC") ? true : false);
		}else{
			listModel.setDesc(true);
		}
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("companyId", sm.getCompanyId());
		if(searchConditions!=null){
			List<String> conditions = new ArrayList<String>();
			InitialUtil.addToCollection(conditions, InitialUtil.split(searchConditions, " ,"));
			map.put("conditions",conditions);
		}
		payInvoiceService.getByCondition(listModel, map);
		resultMap.put("totalProperty", listModel.getTotal());
		resultMap.put("root", listModel.getResult());
		return Action.SUCCESS;
	}
	/* 从支付页面跳过来新建单据 */
	public String  createFormPayable(){	
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=payInvoiceService.createFormPayable(payableIds, customerId, sm);
		return Action.SUCCESS;
	}
	public String getPayInvoiceById(){
		sm= UserHolder.getCurrentSessionMessage();
		resultMap = payInvoiceService.getPayInvoiceById(id,sm);
		resultMap.put("userId", sm.getUserId());
		return Action.SUCCESS;
	}
	/* 删除单据 */
	public String delete() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=payInvoiceService.delete(id);
		return Action.SUCCESS;
	}
	/* 修改单据 */
	public void modify(){
		
	}
	/* 保存单据 */
	@SuppressWarnings("unchecked")
	public String save() {
		sm=UserHolder.getCurrentSessionMessage();
		String[] dateFormats = new String[] {"yyyy-MM-dd"};
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
		PayInvoice payInvoice=(PayInvoice) JSONObject.toBean(JSONObject.fromObject(payInvoiceJson),PayInvoice.class);
		List<PayInvoiceItem> list=(List<PayInvoiceItem>) JSONArray.toCollection(JSONArray.fromObject(payInvoiceItemJson), PayInvoiceItem.class);
		resultMap=payInvoiceService.savePayInvAndItems(payInvoice, list, sm);
		return Action.SUCCESS;
	}
	/* 提交单据 */
	@SuppressWarnings("unchecked")
	public String submit() {
		sm=UserHolder.getCurrentSessionMessage();
		PayInvoice payInvoice=(PayInvoice) JSONObject.toBean(JSONObject.fromObject(payInvoiceJson),PayInvoice.class);
		List<PayInvoiceItem> list=(List<PayInvoiceItem>) JSONArray.toCollection(JSONArray.fromObject(payInvoiceItemJson), PayInvoiceItem.class);
		resultMap=payInvoiceService.submitPayInvoice(payInvoice, list, sm);
		return Action.SUCCESS;
	}
	/* 释放单据 */
	public String release() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=payInvoiceService.release(id, sm);
		return Action.SUCCESS;
	}
	/* 接手单据 */
	public String receive() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=payInvoiceService.receive(id, sm);
		return Action.SUCCESS;
	}
	
	public Map<String, Object> getResultMap() {
		return resultMap;
	}
	public void setStart(Integer start) {
		this.start = start;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public void setDir(String dir) {
		this.dir = dir;
	}
	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}
	public void setSearchConditions(String searchConditions) {
		this.searchConditions = searchConditions;
	}
	@SuppressWarnings("unchecked")
	public void setListModel(ListModel listModel) {
		this.listModel = listModel;
	}
	public void setPayInvoiceService(PayInvoiceService payInvoiceService) {
		this.payInvoiceService = payInvoiceService;
	}
	public void setPayableIds(String payableIds) {
		this.payableIds = payableIds;
	}
	public void setPayInvoiceJson(String payInvoiceJson) {
		this.payInvoiceJson = payInvoiceJson;
	}
	public void setPayInvoiceItemJson(String payInvoiceItemJson) {
		this.payInvoiceItemJson = payInvoiceItemJson;
	}
	public void setSm(SessionMessage sm) {
		this.sm = sm;
	}


}
